package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpLogMapper;
import com.itheima.pojo.EmpLog;
import com.itheima.pojo.PageResult;
import com.itheima.service.EmpLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class EmpLogServiceImpl implements EmpLogService {

    @Autowired
    private EmpLogMapper empLogMapper;

    @Override
    public PageResult<EmpLog> queryLogs(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        Page<EmpLog> logPage = (Page<EmpLog>) empLogMapper.queryLogs();

        PageResult<EmpLog> pageResult = new PageResult<>();
        pageResult.setTotal(logPage.getTotal());
        pageResult.setRows(logPage.getResult());

        return pageResult;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void insertLog(EmpLog empLog) {
        empLogMapper.insertLog(empLog);
    }
}
